package com.li.java1;

import javafx.geometry.HPos;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;

/**
 * @author 喂S别闹
 * @create 2022/4/20-11:55
 * @Version 1.0
 * @Description:Java内置四大核心函数式接口 消费型： Consumer<T>  void  accept(T t)
 * 供给型：Supplier <T> T get()
 * 函数型接口： Function<T,R>   R apply(T t)
 * 断定型接口： Predicate<T> boolean test(t T)
 */
public class LambdaTest2 {

    /**
     * @Date: 2022/4/20 13:59
     * @Auther: 喂S别闹
     * @param:
     * @Return: void
     * @Description:消费型练习
     */
    @Test
    public void test1() {
        happyTime(200, new Consumer<Double>() {
            @Override
            public void accept(Double aDouble) {
                System.out.println("这是以前的写法" + aDouble);
            }
        });

//       Lambda写法
        happyTime(400, money -> System.out.println("lambda" + money));
    }

    public void happyTime(double money, Consumer<Double> con) {
        con.accept(money);
    }


    /**
     * @Date: 2022/4/20 14:02
     * @Auther: 喂S别闹
     * @param:
     * @Return: void
     * @Description:断定型接口
     */
    @Test
    public void test2() {
        List<String> list = Arrays.asList("北京", "南京", "天津", "西京");
        List<String> filterString = filterString(list, new Predicate<String>() {
            @Override
            public boolean test(String s) {
                return s.contains("京");
            }
        });
        System.out.println(filterString);

        //lambda
        List<String> filterString1 = filterString(list, s -> s.contains("京"));
        System.out.println(filterString1);
    }

    //根据给定的规则，过滤集合中的字符串。也有Predicate的方法决定
    public List<String> filterString(List<String> list, Predicate<String> pre) {
        ArrayList<String> filterList = new ArrayList<>();
        for (String s : list) {
            if (pre.test(s)) {
                filterList.add(s);
            }
        }
        return filterList;
    }
}
